package com.cheys.webapp.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cheys.webapp.dao.AdminDao;
import com.cheys.webapp.dao.CaseDao;
import com.cheys.webapp.model.Admin;
import com.cheys.webapp.model.Case;
import com.cheys.webapp.model.Page;
import com.cheys.webapp.model.WebSite;
import com.cheys.webapp.util.LogUtil;
import com.cheys.webapp.util.StringUtil;
@Service
public class AdminService implements IService<Admin> {
	@Autowired
	private AdminDao dao;
	@Autowired
	private CaseDao caseDao;
	
	public Admin getAdminInfo(String adminName,String password) throws Exception{
		Admin info = this.dao.getAdminInfo(adminName, password);
		return info;
	}

	public int update(Admin t) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer();
		if(StringUtil.isNotBlank(t.getAdminName())){
			sb.append(" set adminName = '"+t.getAdminName()+"'");
			sb.append(",");
		}
		if(StringUtil.isNotBlank(t.getAdminPwd())){
			sb.append(" adminPwd = '"+t.getAdminPwd()+"'");
			sb.append(",");
		}
		if(sb.length() == 0){
			return 0;
		}
		if(sb.toString().endsWith(",")){
			sb.deleteCharAt(sb.lastIndexOf(","));
		}
		String sql = "update "+Admin.TABLE_NAME+sb.toString()+" where id="+t.getId();
		return this.dao.update(sql);
	}

	public int delete(int[] id) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	public Admin selectOne(int id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Admin> select(Admin t, Page p) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	public WebSite getWebsitInfo() throws Exception {
		// TODO Auto-generated method stub
		return this.dao.selectWebsiteInfo();
	}
	public int update(WebSite website) throws Exception {
		// TODO Auto-generated method stub
		WebSite info = this.dao.selectWebsiteInfo();
		StringBuffer sb = new StringBuffer();
		if(StringUtil.isNotBlank(website.getTitle()) && !website.getTitle().equals(info.getTitle())){
			sb.append(" title='"+website.getTitle()+"'");
			sb.append(",");
		}
		if(StringUtil.isNotBlank(website.getLogo()) && !website.getLogo().equals(info.getLogo())){
			sb.append(" logo='"+website.getLogo()+"'");
			sb.append(",");
		}
		if(StringUtil.isNotBlank(website.getDomain()) && !website.getDomain().equals(info.getDomain())){
			sb.append(" domain='"+website.getDomain()+"'");
			sb.append(",");
		}
		if(StringUtil.isNotBlank(website.getKeyword()) && !website.getKeyword().equals(info.getKeyword())){
			sb.append(" keyword='"+website.getKeyword()+"'");
			sb.append(",");
		}
		if(StringUtil.isNotBlank(website.getDescript()) && !website.getDescript().equals(info.getDescript())){
			sb.append(" descript='"+website.getDescript()+"'");
			sb.append(",");
		}
		if(StringUtil.isNotBlank(website.getOwner()) && !website.getOwner().equals(info.getOwner())){
			sb.append(" owner='"+website.getOwner()+"'");
			sb.append(",");
		}
		if(StringUtil.isNotBlank(website.getPhone()) && !website.getPhone().equals(info.getPhone())){
			sb.append(" phone='"+website.getPhone()+"'");
			sb.append(",");
		}
		if(StringUtil.isNotBlank(website.getTelphone()) && !website.getTelphone().equals(info.getTelphone())){
			sb.append(" telphone='"+website.getTelphone()+"'");
			sb.append(",");
		}
		if(StringUtil.isNotBlank(website.getEmail()) && !website.getEmail().equals(info.getEmail())){
			sb.append(" email='"+website.getEmail()+"'");
			sb.append(",");
		}
		if(StringUtil.isNotBlank(website.getAddress()) && !website.getAddress().equals(info.getAddress())){
			sb.append(" address='"+website.getAddress()+"'");
			sb.append(",");
		}
		if(StringUtil.isNotBlank(website.getFooter()) && !website.getFooter().equals(info.getFooter())){
			sb.append(" footer='"+website.getFooter()+"'");
			sb.append(",");
		}
		if(sb.length() == 0){
			return 0;
		}
		if(sb.toString().endsWith(",")){
			sb.deleteCharAt(sb.lastIndexOf(","));
		}
		String sql = "update "+WebSite.TABLE_NAME+" set " + sb.toString() +" where id="+info.getId();
		return this.dao.update(sql);
	}
	
	public int save(Admin[] t) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

}
